package com.ford.trade.service.impl;

import com.ford.trade.entity.AoKeGame;
import com.ford.trade.repository.AoKeGameRepository;
import com.ford.trade.service.AoKeGameService;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

/**
 * @author: ford
 * @date: 2022/4/25 12:03
 */
@Service
public class AoKeGameServiceImpl implements AoKeGameService {

    @Autowired
    private AoKeGameRepository aoKeGameRepository;

    @Override
    public void save(AoKeGame game) {

        AoKeGame byIndex = aoKeGameRepository.findByIndexId(game.getIndexId());
        if (null != byIndex) {
            game.setId(byIndex.getId());
        }
        aoKeGameRepository.save(game);
    }

    @Override
    public List<AoKeGame> findAllByDateAfter(String date) {
        return aoKeGameRepository.findAllByDateAfter(date);
    }

    @Override
    public List<AoKeGame> findAllByDateAfterAndType(String date, String type) {
        return aoKeGameRepository.findAllByDateAfterAndType(date, type);
    }

    @Override
    public AoKeGame findByIndexId(Integer indexId) {
        return aoKeGameRepository.findByIndexId(Long.valueOf(indexId));
    }

    @Override
    public List<AoKeGame> findAllByDateBetten(String date) throws ParseException {
        Date addDays = DateUtils.addDays(DateUtils.parseDate(date, "yyyy-MM-dd HH:mm:ss"), 1);
        String format = DateFormatUtils.format(addDays, "yyyy-MM-dd HH:mm:ss");
        return aoKeGameRepository.findByDateBetween(date, format);
    }
}
